namespace Quiki.ExpressionParsers
{
    public class ExternalLinkExpressionParser : BinaryWikiTokenInlineExpressionParser
    {
        public ExternalLinkExpressionParser () : base ("[", "]", ElementConstructor)
        {
        }

        private static WikiTextElement ElementConstructor(string text)
        {
            WikiTextElement wikiTextElement = new WikiTextElement(WikiTextElementType.ExternalLink);
            wikiTextElement.Terminal = true;

            if (text.Contains(" "))
                SplitLinkFromName(wikiTextElement, text);
            else
                wikiTextElement.Properties.Add ("url", text);

            return wikiTextElement;
        }

        private static void SplitLinkFromName(WikiTextElement element, string text)
        {
            int i = text.IndexOf(' ');
            element.Properties.Add("url", text.Substring(0, i));
            element.Properties.Add ("text", text.Substring (i + 1).Trim ());
        }
    }
}